﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FRW.Common.Configuration;

namespace Money.Plugins.Import.TescoImportV2
{
    public class Configuration : ImportConfiguration, IConfiguration
    {
        public int FieldCount { get; set; }
        public string MatchString { get; set; }
        public CategoryMappings CategoryMappings { get; set; }
        public List<TransactionMatch> TransactionMappings { get; set; }
        public bool ApplyNameTidy { get; set; }

        public Configuration()
        {
        }

        public void DefaultConfiguration()
        {
            FieldCount = 11;
            MatchString = "^(?<date>[0-9]+/[0-9]+/[0-9]+)\t(?<postdate>[0-9]+/[0-9]+/[0-9]+)\t(?<amount>.*?)\t\"(?<desc>.*?)\"\t(?<location>.*?)\t(?<country>.*?)\t(?<zip>.*?)\t(?<id>.*?)\t(?<debit>.*?)\t(?<cat>.*?)$";
            CategoryMappings = new CategoryMappings();
        }

        public void CheckConfiguration()
        {
            if (FieldCount == 0)
            {
                FieldCount = 7;
            }

            List<CategoryMapping> removeList = new List<CategoryMapping>();

            // Lets check that none of the categories have been deleted, if so we need to remove our mapping
            foreach (CategoryMapping mapping in CategoryMappings)
            {
                if (Categories.CategoryList != null)
                {
                    string categoryName = Categories.CategoryList.GetCategoryName(mapping.CategoryId);

                    if (String.IsNullOrWhiteSpace(categoryName) || categoryName.Equals("Unknown", StringComparison.InvariantCultureIgnoreCase))
                    {
                        // delete the mapping as it cannot point to a category that no longer exists
                        removeList.Add(mapping);
                    }
                }
            }

            foreach (CategoryMapping mapping in removeList)
            {
                CategoryMappings.Remove(mapping);
            }

            List<TransactionMatch> removeTransactionMappingList = new List<TransactionMatch>();

            // Lets check that none of the categories have been deleted, if so we need to remove our transaction matching
            foreach (TransactionMatch mapping in TransactionMappings)
            {
                if (Money.Categories.CategoryList != null)
                {
                    if (mapping.OriginalCategoryId != null)
                    {
                        string categoryName = Money.Categories.CategoryList.GetCategoryName(Convert.ToInt32(mapping.OriginalCategoryId));

                        if (String.IsNullOrWhiteSpace(categoryName) || categoryName.Equals("Unknown", StringComparison.InvariantCultureIgnoreCase))
                        {
                            // delete the mapping as it cannot point to a category that no longer exists
                            removeTransactionMappingList.Add(mapping);
                        }
                    }
                }
            }

            foreach (TransactionMatch mapping in removeTransactionMappingList)
            {
                TransactionMappings.Remove(mapping);
            }
        }
    }
}
